home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / ka9q / kit_src / inputdb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-03  |  10.7 KB  |  538 lines

  1. /*    (C) Copyright 1991 Dave Fritsche (wb8zxu), All Rights Reserved.
  2.  * 
  3.  *    Redistribution and use in source and binary forms are permitted for
  4.  *    non-commercial use, provided that the above copyright notice and this
  5.  *    paragraph are duplicated in all such forms.  THIS SOFTWARE IS PROVIDED
  6.  *    ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  7.  *    WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND
  8.  *    FITNESS FOR A PARTICULAR PURPOSE.
  9.  */
  10. /*bdoc
  11.  *    Function "INPUTDB"
  12.  *
  13.  *    Written:  Dave Fritsche
  14.  *    Date:  July, 1987
  15.  *
  16.  *    A function to get input from the console according to an input
  17.  *    format pattern.  Syntax:
  18.  *        input("ctrl-string", result-string)
  19.  *    where the "ctrl-string" supplies the format to be used during input
  20.  *    and the "result-string" is what was entered before a <CR>.  The format
  21.  *    characters are:
  22.  *        @ == Alphabetic characters only are allowed in this position
  23.  *        # == Numeric characters only are allowed in this position
  24.  *        | == Numeric characters and "space" are allowed
  25.  *        ? == Alpha/Numeric characters only are allowed in this position
  26.  *        * == Any character is allowed in this position
  27.  *    All other characters are printed literally on the screen as they
  28.  *    are encountered in the input process.  A couple of special keys
  29.  *    are recognized and processed by this routine:  the backspace (delete)
  30.  *    key, the left-arrow key, and the right arrow key.  The arrow keys will
  31.  *    move the cursor around the field and will allow over-typing data.
  32.  *    If deletion is attempted on a literal ctrl-string character, the cursor
  33.  *    is moved backwards until it encounters the first "variable" character
  34.  *    to erase.  A number of keys listed below will end input and the
  35.  *    specific function key that ended input will be returned by the
  36.  *    function.
  37.  *    Example:
  38.  *        key = input("(###) ###-####", phonenum);
  39.  *    would only allow numeric input in the positions shown (it is assumed
  40.  *    that the programmer would have put an empty "target" field on the
  41.  *    screen [like:  (   )    -    ] and positioned the cursor in front of
  42.  *    the first "(" before calling the input function).
  43.  *    When "PRINTOPS" is defined below, this function will use whatever
  44.  *    is in the "result-string" as a 'seed' for the input function.  In
  45.  *    other words, the contents of the string will be printed on the
  46.  *    screen, input and output pointers will be set to the beginning of this
  47.  *    string.  This allows the user to escape from the input function,
  48.  *    then later come back in and change/update the field.
  49.  *    The input function will return a value depending on how the input
  50.  *    function is terminated.  Return values are:
  51.  *        1 -- The "Up arrow" cursor key was pressed
  52.  *        2 -- The "Down arrow" cursor key was pressed
  53.  *        4 -- The "Shift-Tab" key was pressed
  54.  *        8 -- The "Tab" key was pressed
  55.  *        16 -- Carriage return (or Line feed) was entered
  56.  *        32 -- Escape key pressed
  57.  *        64 -- "F1" key
  58.  *        128 -- "F2" key
  59.  *    This capability is enabled by defining "TM220ARROW" (for Ampex-220
  60.  *    terminals) or "PCARROW" (for IBM-pc compatibles with ANSI driver)
  61.  *    below.
  62.  edoc*/
  63.  
  64. #include <stdio.h>
  65. #include <ctype.h>
  66.  
  67. /* #define UNIX            * Define to use UNIX "ugetch()" calls */
  68. #define    PCARROW            /* Define to use cursor keys on PC-clone */
  69. /* #define TM220ARROW        * Define to use cursor keys on TM220 */
  70. #define PRINTOPS        /* Define to enable use of previous output */
  71.  
  72. #ifdef UNIX
  73. #include "ugetch.h"
  74. #endif
  75.  
  76. int inputdb(ips, ops)
  77. char ips[], ops[];
  78. {
  79.     int ch, ip, op, ilen, stat, dot, n;
  80.  
  81.     dot = 0;
  82.     ilen = strlen(ips);
  83.     ip = op = 0;
  84.     stat = 0;
  85.  
  86. #ifdef PRINTOPS
  87.     if ( (strlen(ops) > 0) && (strlen(ops) <= ilen) )
  88.     {
  89.         op = ip = strlen(ops);
  90. #ifdef CPRINTF
  91.         cprintf("%s", ops);
  92. #else
  93.         printf("%s", ops);
  94. #endif
  95.         for (n = 0; n < op; n++)
  96.             if (ops[n] == '.')
  97.                 dot += 1;
  98.     }
  99. #endif
  100.  
  101.     for (n = op; n < ilen; n++)
  102.         ops[n] = ' ';
  103.     ops[ilen] = NULL;
  104.  
  105.     for (n = op; n > 0; n--)
  106.     {
  107. #ifdef CPRINTF
  108.         cprintf("%c", 8);
  109. #else
  110.         printf("%c", 8);
  111. #endif
  112.         op -= 1;
  113.         ip -= 1;
  114.     }
  115.  
  116.     while (stat == 0)
  117.     {
  118.         if ( (ips[ip] != '@') &&
  119.             (ips[ip] != '#') &&
  120.             (ips[ip] != '|') &&
  121.             (ips[ip] != '?') &&
  122.             (ips[ip] != '*') &&
  123.             (ips[ip] != NULL) )
  124.         {
  125. #ifdef CPRINTF
  126.             cprintf("%c", ips[ip]);
  127. #else
  128.             printf("%c", ips[ip]);
  129. #endif
  130.             ops[op] = ips[ip];
  131.             op += 1;
  132.             ip += 1;
  133.         }
  134.         else
  135.         {
  136. #ifdef UNIX
  137.             ch = ugetch();
  138. #else
  139.             ch = getch();
  140. #endif
  141.             if ( (ch == 13) || (ch == 10) )
  142.             {
  143.                 stat = 1;
  144.                 continue;
  145.             }
  146.  
  147.             else if (ch == 9)
  148.             {
  149.                 stat = 3;
  150.                 continue;
  151.             }
  152. #ifdef PCARROW
  153.             else if (ch == 0x1b)
  154.             {
  155.                 stat = 2;
  156.                 continue;
  157.             }
  158.             else if (ch == 0)
  159.             {
  160.                 ch = getch();
  161.                 switch(ch)
  162.                 {
  163.                 case 0x0f:
  164.                     stat = 4;    /* SHFT-TAB */
  165.                     break;
  166.                 case 'H':
  167.                     stat = 100;    /* UP arrow */
  168.                     break;
  169.                 case 'P':
  170.                     stat = 101;    /* DOWN arrow */
  171.                     break;
  172.                 case 'K':
  173.                     if (ip <= 0)    /* LEFT arrow */
  174.                     {
  175.                         ip = op = 0;
  176. #ifdef CPRINTF
  177.                         cprintf("%c", 7);
  178. #else
  179.                         printf("%c", 7);
  180. #endif
  181.                         continue;
  182.                     }
  183.                     while ( (ip > 0) &&
  184.                             (ips[ip-1] != '@') &&
  185.                             (ips[ip-1] != '#') &&
  186.                             (ips[ip-1] != '|') &&
  187.                             (ips[ip-1] != '?') &&
  188.                             (ips[ip-1] != '*') &&
  189.                             (ips[ip-1] != NULL) )
  190.                     {
  191.                         ip -= 1;
  192.                         op -= 1;
  193. #ifdef CPRINTF
  194.                         cprintf("%c", 8);
  195. #else
  196.                         printf("%c", 8);
  197. #endif
  198.                     }
  199.                     if (ip <= 0)
  200.                     {
  201.                         ip = op = 0;
  202. #ifdef CPRINTF
  203.                         cprintf("%c", 7);
  204. #else
  205.                         printf("%c", 7);
  206. #endif
  207.                         continue;
  208.                     }
  209.                     ip -= 1;
  210.                     op -= 1;
  211. #ifdef CPRINTF
  212.                     cprintf("%c", 8);
  213. #else
  214.                     printf("%c", 8);
  215. #endif
  216.                     continue;
  217.                     break;
  218.                 case 'M':        /* RIGHT arrow */
  219.                     if (ips[ip] == NULL)
  220.                     {
  221. #ifdef CPRINTF
  222.                         cprintf("%c", 7);
  223. #else
  224.                         printf("%c", 7);
  225. #endif
  226.                         continue;
  227.                     }
  228.                     if ( (ips[ip] != '@') &&
  229.                             (ips[ip] != '#') &&
  230.                             (ips[ip] != '|') &&
  231.                             (ips[ip] != '?') &&
  232.                             (ips[ip] != '*') &&
  233.                             (ips[ip] != NULL) )
  234. #ifdef CPRINTF
  235.                         cprintf("%c", ips[ip]);
  236. #else
  237.                         printf("%c", ips[ip]);
  238. #endif
  239.                     else
  240. #ifdef CPRINTF
  241.                         cprintf("%c", ops[op]);
  242. #else
  243.                         printf("%c", ops[op]);
  244. #endif
  245.                     ip += 1;
  246.                     op += 1;
  247.                     while ( (ips[ip] != NULL) &&
  248.                             (ips[ip] != '@') &&
  249.                             (ips[ip] != '#') &&
  250.                             (ips[ip] != '|') &&
  251.                             (ips[ip] != '?') &&
  252.                             (ips[ip] != '*') )
  253.                     {
  254. #ifdef CPRINTF
  255.                         cprintf("%c", ips[ip]);
  256. #else
  257.                         printf("%c", ips[ip]);
  258. #endif
  259.                         ip += 1;
  260.                         op += 1;
  261.                     }
  262.                     if (ips[ip] == NULL)
  263.                     {
  264.                         while ( (ip > 0) &&
  265.                             (ips[ip-1] != '@') &&
  266.                             (ips[ip-1] != '#') &&
  267.                             (ips[ip-1] != '|') &&
  268.                             (ips[ip-1] != '?') &&
  269.                             (ips[ip-1] != '*') &&
  270.                             (ips[ip-1] != NULL) )
  271.                         {
  272.                             ip -= 1;
  273.                             op -= 1;
  274. #ifdef CPRINTF
  275.                             cprintf("%c", 8);
  276. #else
  277.                             printf("%c", 8);
  278. #endif
  279.                         }
  280.                     }
  281.                     break;
  282.                 case ';':
  283.                     stat = 102;    /* F1 key */
  284.                     break;
  285.                 case '<':
  286.                     stat = 103;    /* F2 key */
  287.                     break;
  288.                 default:
  289. #ifdef CPRINTF
  290.                     cprintf("%c", 7);
  291. #else
  292.                     printf("%c", 7);
  293. #endif
  294.                     break;
  295.                 }
  296.                 continue;
  297.             }
  298. #endif
  299. #ifdef TM220ARROW
  300.             else if (ch == 0x1b)
  301.             {
  302. #ifdef UNIX
  303.                 if (ugetch() != '[')
  304. #else
  305.                 if (getch() != '[')
  306. #endif
  307. #ifdef CPRINTF
  308.                     cprintf("%c%c", 7, 7);
  309. #else
  310.                     printf("%c%c", 7, 7);
  311. #endif
  312.                 else
  313.                 {
  314. #ifdef UNIX
  315.                     switch (ugetch())
  316. #else
  317.                     switch (getch())
  318. #endif
  319.                     {
  320.                     case 'A':
  321.                         stat = 100;
  322.                         break;
  323.                     case 'B':
  324.                         stat = 101;
  325.                         break;
  326.                     case 'C':
  327.                         stat = 105;
  328.                         break;
  329.                     case 'D':
  330.                         stat = 104;
  331.                         break;
  332.                     default:
  333. #ifdef CPRINTF
  334.                         cprintf("%c", 7);
  335. #else
  336.                         printf("%c", 7);
  337. #endif
  338.                         break;
  339.                     }
  340.                 }
  341.                 continue;
  342.             }
  343. #endif
  344.             else if ( (ch == 8) || (ch == 0x7f) )
  345.             {
  346.                 if (ip <= 0)
  347.                 {
  348.                     ip = op = 0;
  349. #ifdef CPRINTF
  350.                     cprintf("%c", 7);
  351. #else
  352.                     printf("%c", 7);
  353. #endif
  354.                     continue;
  355.                 }
  356.                 while ( (ip > 0) &&
  357.                         (ips[ip-1] != '@') &&
  358.                         (ips[ip-1] != '#') &&
  359.                         (ips[ip-1] != '|') &&
  360.                         (ips[ip-1] != '?') &&
  361.                         (ips[ip-1] != '*') &&
  362.                         (ips[ip-1] != NULL) )
  363.                 {
  364.                     ip -= 1;
  365.                     op -= 1;
  366. #ifdef CPRINTF
  367.                     cprintf("%c", 8);
  368. #else
  369.                     printf("%c", 8);
  370. #endif
  371.                 }
  372.                 if (ip <= 0)
  373.                 {
  374.                     ip = op = 0;
  375. #ifdef CPRINTF
  376.                     cprintf("%c", 7);
  377. #else
  378.                     printf("%c", 7);
  379. #endif
  380.                     continue;
  381.                 }
  382.                 ip -= 1;
  383.                 op -= 1;
  384.                 if ( (ops[op] == '.') && (dot > 0) )
  385.                     dot -= 1;
  386. #ifdef CPRINTF
  387.                 cprintf("%c %c", 8, 8);
  388. #else
  389.                 printf("%c %c", 8, 8);
  390. #endif
  391.                 ops[op] = ' ';
  392.                 continue;
  393.             }
  394.             switch (ips[ip])
  395.             {
  396.             case NULL:
  397. #ifdef CPRINTF
  398.                 cprintf("%c", 7);
  399. #else
  400.                 printf("%c", 7);
  401. #endif
  402.                 break;
  403.             case '@':
  404.                 if (isalpha(ch) == 0)
  405.                 {
  406. #ifdef CPRINTF
  407.                     cprintf("%c", 7);
  408. #else
  409.                     printf("%c", 7);
  410. #endif
  411.                     break;
  412.                 }
  413. #ifdef CPRINTF
  414.                 cprintf("%c", ch);
  415. #else
  416.                 printf("%c", ch);
  417. #endif
  418.                 ops[op] = ch & 0xff;
  419.                 op += 1;
  420.                 ip += 1;
  421.                 break;
  422.             case '#':
  423.                 if (isdigit(ch) == 0)
  424.                 {
  425. #ifdef CPRINTF
  426.                     cprintf("%c", 7);
  427. #else
  428.                     printf("%c", 7);
  429. #endif
  430.                     break;
  431.                 }
  432. #ifdef CPRINTF
  433.                 cprintf("%c", ch);
  434. #else
  435.                 printf("%c", ch);
  436. #endif
  437.                 ops[op] = ch & 0xff;
  438.                 op += 1;
  439.                 ip += 1;
  440.                 break;
  441.             case '|':
  442.                 if ( (isdigit(ch) == 0) &&
  443.                     (ch != ' ') &&
  444.                     ( (ch != '.') ||
  445.                     ((ch == '.') && (dot != 0)
  446.                     && (ops[op] != '.')) ) )
  447.                 {
  448. #ifdef CPRINTF
  449.                     cprintf("%c", 7);
  450. #else
  451.                     printf("%c", 7);
  452. #endif
  453.                     break;
  454.                 }
  455.                 if ( (ch == '.') && (ops[op] != '.') )
  456.                     dot += 1;
  457.                 if ( (ch != '.') && (ops[op] == '.') )
  458.                     dot -= 1;
  459. #ifdef CPRINTF
  460.                 cprintf("%c", ch);
  461. #else
  462.                 printf("%c", ch);
  463. #endif
  464.                 ops[op] = ch & 0xff;
  465.                 op += 1;
  466.                 ip += 1;
  467.                 break;
  468.             case '?':
  469.                 if (isalnum(ch) == 0)
  470.                 {
  471. #ifdef CPRINTF
  472.                     cprintf("%c", 7);
  473. #else
  474.                     printf("%c", 7);
  475. #endif
  476.                     break;
  477.                 }
  478. #ifdef CPRINTF
  479.                 cprintf("%c", ch);
  480. #else
  481.                 printf("%c", ch);
  482. #endif
  483.                 ops[op] = ch & 0xff;
  484.                 op += 1;
  485.                 ip += 1;
  486.                 break;
  487.             case '*':
  488. #ifdef CPRINTF
  489.                 cprintf("%c", ch);
  490. #else
  491.                 printf("%c", ch);
  492. #endif
  493.                 ops[op] = ch & 0xff;
  494.                 op += 1;
  495.                 ip += 1;
  496.                 break;
  497.             default:
  498.                 break;
  499.             }
  500.         }
  501.     }
  502.     chktxt(ops);
  503.  
  504.     switch(stat)
  505.     {
  506.     case 2:
  507.         return(32);
  508.         break;
  509.     case 3:
  510.         return(8);
  511.         break;
  512.     case 4:
  513.         return(4);
  514.         break;
  515.     case 100:
  516.         return(1);
  517.         break;
  518.     case 101:
  519.         return(2);
  520.         break;
  521.     case 102:
  522.         return(64);
  523.         break;
  524.     case 103:
  525.         return(128);
  526.         break;
  527.     case 104:
  528.         return(4);
  529.         break;
  530.     case 105:
  531.         return(8);
  532.         break;
  533.     default:
  534.         return(16);
  535.         break;
  536.     }
  537. }
  538.